#!/usr/bin/env bash

set -e -u -x
source ./tasks/helpers.sh

task_working_dir

# Copy the admin-ui app locally so that we can build locally (outside of the
# source directory) without affecting the "node_modules" setup in the source
# directory.
mkdir -p app
cd app
rsync -a \
  --itemize-changes \
  --delete \
  --delete-excluded \
  --filter=":- $SOURCE_DIR/src/api-umbrella/admin-ui/.gitignore" \
  "$SOURCE_DIR/src/api-umbrella/admin-ui/" \
  ./

# Symlink the local node_modules to the system-wide node directory.
#
# Note, we create a `.pnpm` directory inside (even if pnpm isn't being used,
# since it won't hurt anything), since it seems to sidestep this issue of
# install failing if node_modules is a symlink:
# https://github.com/pnpm/pnpm/issues/7690
node_modules_dir="$WORK_DIR/tasks/app-deps/admin-ui/pnpm/_persist/node_modules"
mkdir -p "${node_modules_dir}/.pnpm"
ln -s "$node_modules_dir" ./node_modules

# Install the "dist" result into the task's special _persist directory so that
# these dependencies for the tasks/app/core task are still present if core
# needs to be re-run, but not this task.
mkdir -p ../_persist
# Set JOBS=1 to fix parallel issues in CI environment:
# https://github.com/emberjs/ember.js/issues/15641#issuecomment-328496408
PATH="$DEV_PATH" JOBS=1 ./node_modules/.bin/ember build \
  --environment=production \
  --output-path=../_persist/dist

# Copy the index.html to a separate location for the test suite, and modify the
# inline config in the HTML file to set "integrationTestMode: true".
rm -rf "$TEST_INSTALL_PREFIX/admin-ui"
mkdir -p "$TEST_INSTALL_PREFIX/admin-ui"
cp ../_persist/dist/index.html "$TEST_INSTALL_PREFIX/admin-ui/index.html"
sed -i -e 's#<meta name="api-umbrella-admin-ui/config/environment" content="%7B%22#<meta name="api-umbrella-admin-ui/config/environment" content="%7B%22integrationTestMode%22%3Atrue%2C%22#' "$TEST_INSTALL_PREFIX/admin-ui/index.html"

stamp
